//
//  EnterCertifiViewController.m
//  OfferJob
//
//  Created by 河南动力无限科技有限公司 on 17/7/20.
//  Copyright © 2017年 郑州动力无限科技. All rights reserved.
//

#import "EnterCertifiViewController.h"
#import "CellView.h"
@interface EnterCertifiViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>

@property (nonatomic,strong)UIScrollView* scrollView;
@property (nonatomic,strong) NSString *logo1;
@property (nonatomic,strong) NSMutableDictionary *dic;
@property (nonatomic,assign) NSInteger selectButton;
@end

@implementation EnterCertifiViewController

- (void)viewDidLoad {
    self.title=@"企业认证";
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _dic=[[NSMutableDictionary alloc]init];
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    _scrollView.backgroundColor=backgroundcolor;
    //    [_scrollView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_scrollView];
    
    
    NSArray *MArr=@[@"姓名:",@"身份证号:",@"现居住:",@"现居住:",@"上传营业执照"];
    NSArray *VArr=@[@"请输入您的姓名",@"请输入您的身份证号",@"请输入您的现居住地",@"现居住:",@""];
    float setY=0;
    for (int i=0; i<[MArr count]; i++) {
        CellView *cell=[[CellView alloc]initWithFrame:CGRectMake(0*kAppScale, setY, self.view.width, 44*kAppScale)];
        cell.titleLabel.textColor=blackTextColor;
        cell.titleLabel.text=MArr[i];
        cell.titleLabel.textAlignment=NSTextAlignmentLeft;

        UITextField *textF=[[UITextField alloc]initWithFrame:CGRectMake(80*kAppScale, 7*kAppScale, 280*kAppScale, cell.height-10*kAppScale)];
        textF.font=[UIFont systemFontOfSize:13*kAppScale];
        textF.placeholder=VArr[i];
//        textF.text=arr[i];
        textF.textColor=blackTextColor;
        textF.tag=10+i;
        [cell addSubview:textF];
        
        if (i==4) {
            cell.userInteractionEnabled=NO;
           cell.titleLabel.frame=CGRectMake(10*kAppScale, 5*kAppScale, self.view.width, 44*kAppScale);
        }
        
        [_scrollView addSubview:cell];
        setY=cell.bottom;
    }

    
    UIView * view=[[UIView alloc]initWithFrame:CGRectMake(0*kAppScale, setY, SCREEN_WIDTH, 200*kAppScale)];
    view.backgroundColor=[UIColor whiteColor];
    [_scrollView addSubview:view];
    NSArray* image=@[@"icon_yyzz",@"icon_yyzz"];
    for ( int i = 0 ; i < 2; i ++) {
        UIImageView*TwoVie=[[UIImageView alloc]initWithFrame:CGRectMake(180*kAppScale*i+15*kAppScale, 10*kAppScale,165*kAppScale, 180*kAppScale)];
        TwoVie.image=[UIImage imageNamed:image[i]];
        [view  addSubview:TwoVie ];
        TwoVie.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        TwoVie.layer.borderWidth = 0.5;
        TwoVie.layer.cornerRadius = 3;
        TwoVie.tag=1000+i;
        TwoVie.userInteractionEnabled=YES;
        
        
        if (i==1) {
            TwoVie.userInteractionEnabled=NO;
        }
        UITapGestureRecognizer* singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tuPianBtnClick)];
        singleRecognizer.numberOfTapsRequired = 1; // 单击
        [TwoVie addGestureRecognizer:singleRecognizer];
        
    }
    
    
    
    UIButton *nextBtn=[[UIButton alloc]initWithFrame:CGRectMake(10*kAppScale,view.bottom+30*kAppScale,SCREEN_WIDTH-20*kAppScale,44*kAppScale)];
    nextBtn.backgroundColor=NavCOLOR;
    [nextBtn setTitle:@"提交验证" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextBtn.titleLabel.font=[UIFont systemFontOfSize:12*kAppScale];
    [nextBtn addTarget:self action:@selector(nextBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:nextBtn];
    
    _scrollView.contentSize =CGSizeMake(SCREEN_WIDTH, nextBtn.bottom);

}
- (void)nextBtnEvent{


}
#pragma mark -- 点击事件
- (void)tuPianBtnClick{
    [self.view endEditing:YES];
    _selectButton =1000;
    
    [self takePhotoEvent:nil];
}

#pragma mark -- 选择照片
-(void)takePhotoEvent:(UITapGestureRecognizer *)tap{
    
    
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"请选择以下方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //相机
    UIAlertAction *PaiZhaoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.delegate = self;
            //            imagePicker.allowsEditing = YES;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
    }];
    //相册
    UIAlertAction *xiangCeAction = [UIAlertAction actionWithTitle:@"从相册中获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"相册");
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }];
    //取消
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [PaiZhaoAction setValue:blackTextColor forKey:@"_titleTextColor"];
    [xiangCeAction setValue:blackTextColor forKey:@"_titleTextColor"];
    [cancleAction setValue:redTextColor forKey:@"_titleTextColor"];
    [alertControl addAction:PaiZhaoAction];
    [alertControl addAction:xiangCeAction];
    [alertControl addAction:cancleAction];
    [self presentViewController:alertControl animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *newImage = [[UIImage alloc]init];
    newImage=[self imageWithImageSimple:image scaledToSize:CGSizeMake(600, 600)];
    NSData *date = UIImageJPEGRepresentation(newImage, .5);
    UIImageView* paiZhaoBtn=(UIImageView*)[self.view viewWithTag:_selectButton];
    //    [paiZhaoBtn setBackgroundImage:image forState:0];
    int  i=0;
    
    
    UIImageView *imgV =[self.view viewWithTag:44+i];
    imgV.hidden=YES;
    
    UILabel *titleLB =[self.view viewWithTag:55+i];
    titleLB.hidden=YES;
    
    i++;
    
    
    paiZhaoBtn.image=image;
    NSLog(@"我的导入到若%ld",(long)_selectButton);
    switch (_selectButton) {
        case 1000:
            
            _logo1= [date base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            
            break;

        default:
            break;
    }
    
    //    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIImagePickerDelegate
-(UIImage *) imageWithImageSimple:(UIImage*) image scaledToSize:(CGSize) newSize{
    newSize.height=image.size.height*(newSize.width/image.size.width);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  newImage;
}

- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
{
    NSData* imageData = UIImagePNGRepresentation(tempImage);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPathToFile atomically:NO];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
